package com.xb.system.controller;

import com.xb.system.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 广告管理Controller（前台系统）
 * 
 * @author xb
 * @date 2025-05-28
 */
@RestController
@RequestMapping("/checksystem/advertisement")
public class AdvertisementController {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 获取启用的广告（代理到checksystem后台）
     * 前台后端和后台后端都在本地，直接用localhost访问
     */
    @GetMapping("/enabled")
    public Result getEnabled() {
        try {
            // 调用本地checksystem后台的API（都在本地部署）
            String url = "http://localhost:8081/checksystem/advertisement/enabled";
            // 直接返回Map，避免字段不匹配问题
            @SuppressWarnings("unchecked")
            java.util.Map<String, Object> response = restTemplate.getForObject(url, java.util.Map.class);
            
            // 添加日志查看返回内容
            System.out.println("=== 广告API响应 ===");
            System.out.println("完整响应: " + response);
            if (response != null) {
                System.out.println("response.code: " + response.get("code"));
                System.out.println("response.msg: " + response.get("msg"));
                System.out.println("response.data: " + response.get("data"));
            }
            System.out.println("==================");
            
            if (response != null && response.get("data") != null) {
                return Result.ok(response.get("data"));
            } else {
                return Result.fail("未获取到广告数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("获取广告信息失败: " + e.getMessage());
        }
    }
}
